package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2024/7/19 10:09
 * @apiNote
 */

public class Test03096MinimumLevels {
    public static void main(String[] args) {
        int[] possible = {1,0,1,0};
        int res =minimumLevels(possible);
        System.out.println(res);
    }

    public int minimumLevels1(int[] possible) {
        int n = possible.length;
        int left = 0, right = 0;
        for (int i = n - 1; i >= 0; i--) {
            int temp = possible[i] == 0 ? -1 : 1;
            right += temp;
        }
        for (int i = 0; i < n-1; i++) {
            int temp = possible[i] == 0 ? -1 : 1;
            if (left + temp > right - temp) {
                return i+1;
            } else {
                left += temp;
                right -= temp;
            }
        }
        return -1;
    }

    public static int minimumLevels(int[] possible) {
        int n = possible.length;
        int left = 0, right = 0;
        for (int i = n - 1; i >= 0; i--) {
            right += possible[i] == 0 ? -1 : 1;
        }
        for (int i = 0; i < n; i++) {
            left += possible[i] == 0 ? -1 : 1;
            if (2 * left  > right) {
                return i + 1;
            }
        }
        return -1;
    }
}
